# Function: Provide common data process function packages
# Author: Mark Xiao  2023/2/2

# API Summary:
# Numeric type conversion (for global use)：
	# 1. hex2dec()		convert hex string to dec string
	# 2. dec2hex()		convert dec string to hex string
	# 3. hex2bin()		convert hex string to bin string
	# 4. bin2hex()		convert bin string to hex string
	# 5. bin2dec()		convert bin string to dec string
	# 6. dec2bin()		convert dec string to bin string
	# 7. str2asicii()	convert a str to a table of it's asicii value, if only one bit then return it's asicii value in stead of the table
	# 8. asicii2str()	convert a asicii value to a char

# Data processing (for global use):
	# 1. trim()			remove extra spaces at begin and end of a string

# String data processing (for sequence CSV use):
	# 1. compare()		compare two input string  and return string "PASS/FAIL", Input should be a table including any type of data
	# 2. regex()		regex the input string and return the result, multiple regex results will be connected in one string 
	# 3. split()		split the input string and return the results in a table
	# 4. joint()		joint multiple input string and return the result, input is a table of strings
	# 5. boll2string()	convert bool input value to string type "PASS/FAIL"

	


import re

# Global Variables
checkHex = ("0", "1", "2", "3", "4", "5", "6", "7", "8", "9", "a", "b", "c", "d", "e", "f")
checkDec = ("0", "1", "2", "3", "4", "5", "6", "7", "8", "9")
checkBin = ("0", "1")


# Numeric type conversion (for global use)：
# convert hex string to dec string
def hex2dec(hexString):
	if not hexString[0:2] == "0x":
		print("Invalid hex string type as input")
		return
	for key in hexString[2:]:
		if not key in checkHex:
			print("Invalid hex string type as input")
			return
	return str(int(hexString, 16))


# convert dec string to hex string
def dec2hex(decString):
	for key in decString:
		if not key in checkDec:
			print("Invalid dec string type as input")
			return
	return hex(int(decString))	


# convert hex string to bin string
def hex2bin(hexString):
	if not hexString[0:2] == "0x":
		print("Invalid hex string type as input")
		return
	for key in hexString[2:]:
		if not key in checkHex:
			print("Invalid hex string type as input")
			return
	return bin(int(hexString, 16))


# convert bin string to hex string
def bin2hex(binString):
	if not binString[0:2] == "0b":
		print("Invalid hex string type as input")
		return
	for key in binString[2:]:
		if not key in checkBin:
			print("Invalid hex string type as input")
			return
	return hex(int(binString, 2))	


# convert bin string to dec string
def bin2dec(binString):
	if not binString[0:2] == "0b":
		print("Invalid hex string type as input")
		return
	for key in binString[2:]:
		if not key in checkBin:
			print("Invalid hex string type as input")
			return
	return str(int(binString, 2))	


# convert dec string to bin string
def dec2bin(decString):
	for key in decString:
		if not key in checkDec:
			print("Invalid dec string type as input")
			return
	return bin(int(decString))	


# convert a str to a table of it's asicii value, if only one bit then return it's asicii value in stead of the table
def str2asicii(inputStr):
	result = []
	if not inputStr == "" or None:
		if len(inputStr) == 1:
			print("str2asicii result: ", ord(inputStr))
			return ord(inputStr)
		else:
			for e in inputStr:
				result.append(ord(e))
			print("str2asicii result: ", result)
	else:
		print("Input str is None")
	return result


# convert a asicii value to a char
def asicii2str(asiciiNum):
	resultStr = ""
	if asiciiNum >=0 and asiciiNum <= 127:
		resultStr = chr(asiciiNum)
		print("asicii2str result: ", resultStr)
	else:
		print("Invalid ASICII number as input")
	return resultStr



# Data processing (for global use):
# emove extra spaces at begin and end of a string
def trim(inputStr):
	if not inputStr == "" or None:
		if inputStr[0] == " ":
			return trim(inputStr[1:])   
		elif inputStr[-1] == " ":
			return trim(inputStr[:-1])  
		else:
			return inputStr
	else:
		print("Invalid input string")
		return None





# String data processing (for sequence CSV use):
# compare two input string  and return string "PASS/FAIL", Input should be a table including any type of data
def compare(paraTab):
	inputStr = paraTab["Input"] or []
	print("inputStr including: ", inputStr)
	result = "PASS"
	try:
		tempStr = str(inputStr[0])
	except:
		print("Invalid input string list")
	for key in inputStr:
		if not str(key) == tempStr:
			result = "FAIL"
			return result
	return result


# regex the input string and return the result, multiple regex results will be connected in one string
def regex(paraTab):
	inputStr = paraTab["Input"] or ""
	pattern = paraTab["AdditionalParameters"]["pattern"] or ""
	print("Regex input str: ", inputStr)
	print("Regex pattern: ", pattern)
	result = ""
	respStr = ""
	if not (inputStr or pattern) == "":
		result = re.match(pattern, inputStr, flags = 0)
	else:
		print("Invalid input value or pattern")

	if not result == None:
		for e in result.groups():
			respStr = respStr + e
		print("Regex output result: ", respStr)
	else:
		print("No regex result found")
	return respStr


# split the input string and return the results in a table
def split(paraTab):
	inputStr = paraTab["Input"] or ""
	splitFlag = paraTab["AdditionalParameters"]["splitFlag"] or ""
	print("Split input str: ", inputStr)
	print("Split flag: ", splitFlag)
	result = []
	result = inputStr.split(splitFlag)
	print("Split result: ", result)
	return result


# joint multiple input string and return the result, input is a table of strings
def joint(paraTab):
	inputStr = paraTab["Input"] or ""
	result = ""
	print("Joint input including: ", inputStr)
	for e in inputStr:
		result = result + e
	print("Joint result: ", result)
	return result


# convert bool input value to string type "PASS/FAIL"
def bool2string(paraTab):
	inputBool = paraTab["Input"] or ""
	result = ""
	if inputBool == True:
		result = "PASS"
	else:
		result = "FAIL"
	print("bool2string result: ", result)
	return result



